﻿Public Class FrmDMChucVu
    Dim provider As New DataProvider
    Dim flag As String = 0
    Function LoadDuLieu()
        dtgchucvu.DataSource = New DataTable
        Dim sql As String
        sql = "select * from ChucVu"
        Dim dt As New DataTable
        dt = provider.ExecuteQuery(sql)
        dtgchucvu.DataSource = dt
        Enable(False)
        btnthem.Enabled = True
        btnluu.Enabled = False
        btnsua.Enabled = True
        btnxoa.Enabled = True
        dtgchucvu.Enabled = True
        btnhuy.Enabled = False

    End Function

    Private Sub FrmDMChucVu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDuLieu()

    End Sub

    Private Sub dtgchucvu_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgchucvu.CellContentClick, dtgchucvu.CellContentClick

    End Sub

    Private Sub dtgchucvu_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgchucvu.CellClick, dtgchucvu.CellClick
        Try
            Dim row As Integer = dtgchucvu.CurrentCell.RowIndex
            txtmacv.Text = dtgchucvu.Rows(row).Cells(0).Value
            txttencv.Text = dtgchucvu.Rows(row).Cells(1).Value
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        flag = 1
        Enable(True)
        btnsua.Enabled = False
        btnxoa.Enabled = False
        btnthem.Enabled = False
        dtgchucvu.Enabled = False
        txtmacv.Text = ""
        txttencv.Text = ""
        btnhuy.Enabled = True

    End Sub

    Private Sub btxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click
        Try
            Dim row As Integer = dtgchucvu.CurrentCell.RowIndex
            Dim macv As String
            macv = dtgchucvu.Rows(row).Cells(0).Value
            Dim sql As String
            sql = " DELETE FROM ChucVu where MaCV = '" + macv + "'"
            provider.ExecuteNonQuery(sql)
            LoadDuLieu()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        btnhuy.Enabled = False
        If txtmacv.Text = "" Then
            MessageBox.Show("Chọn 1 chức vụ để sửa ")
        Else
            flag = 2
            Enable(True)
            btnthem.Enabled = False
            btnxoa.Enabled = False
            btnsua.Enabled = False
            txtmacv.Enabled = False

        End If


    End Sub

    Private Sub btluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        Try

            If flag = 1 Then
                Dim macv As String
                Dim tencv As String
                macv = txtmacv.Text
                tencv = txttencv.Text
                Dim sql As String
                sql = "select * from ChucVu where MaCV='" + macv + "'"
                Dim dt As New DataTable
                dt = provider.ExecuteQuery(sql)

                If dt.Rows.Count > 0 Then
                    MessageBox.Show("Trùng Mã Chức Vụ")
                    txtmacv.Focus()
                Else
                    sql = "insert into Chức Vụ values(N'" + macv + "',N'" + tencv + "')"
                    provider.ExecuteNonQuery(sql)
                    LoadDuLieu()
                End If


            ElseIf flag = 2 Then

                Dim macv As String
                Dim tencv As String
                macv = txtmacv.Text
                tencv = txttencv.Text
                Dim sql As String
                sql = "update ChucVu set MaCV = '" + macv + "', TenCV= N'" + tencv + "' WHERE MaCV = '" + macv + "'"
                provider.ExecuteNonQuery(sql)
                LoadDuLieu()
            End If

        Catch ex As Exception
            MessageBox.Show("Không thể lưu!")
        End Try

    End Sub
    Private Sub Enable(ByVal i As Boolean)
        txtmacv.Enabled = i
        txttencv.Enabled = i
        btnluu.Enabled = i
    End Sub


    Private Sub btnhuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhuy.Click
        LoadDuLieu()

    End Sub
End Class